package view.sit
{
	import control.sit.SitControl;
	
	import core.View;
	
	import events.CircleTimerEvent;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormatAlign;
	
	import global.Resource;
	
	import utils.DisplayTool;
	import utils.TextTool;
	
	import view.components.CircleTimer;
	import view.components.CountNumber;
	import view.components.CountTimer;
	import view.components.RemoteHtmlImg;
	
	public class AbsSit extends Sprite implements ISit
	{
		private var _view:View = View.getInstance();
		
		private var autoImg:DisplayObject;
		private var bg:DisplayObject;
		private var title:TextField;
		private var defaultWidth:int = 110;
		private var leftCardBg:DisplayObject;
		private var money:TextField;
		private var state:TextField;
		private var left:TextField;
		private var circleTimer:CircleTimer;
		private var textBg:Shape = new Shape();;
		private var dan:MovieClip;
		private var sitPrompt:SitPrompt;
		private var lastHoldCards:int;
		private var sitControl:SitControl;
		private var _sitId:int;
		private var headImg:RemoteHtmlImg;
		private var vipImg:Bitmap;
		private var currentItemAnimate:DisplayObject;
		
		
		//表计时
		private static var countTimer:CountTimer ;
		
		public function get sitId():int
		{
			return _sitId;
		}
		
		public function set sitId(v:int):void
		{
			_sitId = v;
		}
		
		public function AbsSit(sitControl:SitControl)
		{
			this.sitControl = sitControl;
			initCircleTimer();
			initTitle();
			initBg();
			initTextBg2();
			addChild(textBg);
			initLeftCardBg();
			initOtherText();
			
			initDan();
			initPrompt();
			
			initCountTimer();
			initRemoteImg();
			initAutoImg();
		}
		
		private function initBg():void
		{
			bg = Resource.getImg("panel_sitBg");
			bg.x = 41;
			bg.y = 14; 
			
			addChild(bg);
			
		}
		
		private function initRemoteImg():void
		{
			headImg = new RemoteHtmlImg(50,50);
			headImg.x = bg.x + 5 ;
			headImg.y = bg.y + 3;
			headImg.buttonMode = true;
			addChild(headImg);
			
			headImg.addEventListener(MouseEvent.CLICK,onHeadClick);
			headImg.addEventListener(MouseEvent.MOUSE_OVER,onHeadOver);
			headImg.addEventListener(MouseEvent.MOUSE_OUT,onHeadOut);
		}
		
		private function onHeadOver(event:MouseEvent):void
		{
			sitControl.onSitHeadOver(this._sitId);
		}
		
		private function onHeadOut(event:MouseEvent):void
		{
			sitControl.onSitHeadOut();	
		}
		
		private function onHeadClick(event:MouseEvent):void
		{
			sitControl.onSitHeadClick(this._sitId);
		}
		
		private function initCountTimer():void
		{
			if (countTimer == null)
			{
				countTimer = new CountTimer()
				countTimer.x = 31;
				countTimer.y = 94;
			}
		}
		
		private function initPrompt():void
		{
			sitPrompt = new SitPrompt();
		}
		
		private function initDan():void
		{
			dan = Resource.getEffect("Single");
			dan.y = 86;
			dan.x = 14;
		}
		
		private function initAutoImg():void
		{
			autoImg = Resource.getImg("face_auto");
			autoImg.x = 113;
			autoImg.y = 75;
			addChild(autoImg);
			autoImg.visible = false;
		}
		
		private function initCircleTimer():void
		{
			circleTimer = new CircleTimer();
			circleTimer.x = 29;
			circleTimer.y = 0;
			addChild(circleTimer);
			circleTimer.addEventListener(TimerEvent.TIMER,onCircleTimer);
			circleTimer.addEventListener(CircleTimerEvent.CIRCLE_HINT,onCircleHint);
			//circleTimer.totalTime = 15;
		}
		
		private function onCircleHint(event:CircleTimerEvent):void
		{
			sitControl.sitCircleHint(this._sitId);
		}
		
		private function onCircleTimer(event:TimerEvent):void
		{
			countTimer.update(circleTimer.leftTime.toString());
			
		}
		
		private function initOtherText():void
		{
			money = TextTool.getTextField(80,16,12,0xFFFF00,false,true,TextFormatAlign.CENTER);
			money.x = 31;
			money.y = 73;
			addChild(money);
			
			state = TextTool.getTextField(80,18,12,0xFFFFFF,false,false,TextFormatAlign.CENTER);
			state.x = 31;
			state.y = 93;
			addChild(state);	
			
			left = TextTool.getTextField(28,22,14,0xFFFFFF,false,true);
			left.x = 2;
			left.y = 78;
			left.visible = false;
			addChild(left);
		}
		
		private function initLeftCardBg():void
		{
			leftCardBg = Resource.getImg("panel_leftCardBg");
			leftCardBg.y = 74;
			leftCardBg.visible = false;
			addChild(leftCardBg);
		}
		
		private function initTextBg():void
		{

			with (textBg.graphics)
			{
				clear();
				lineStyle(2,0xFFFFFF,.8);
				beginFill(0,.8)
				drawRoundRect(31,74,80,16,15);
				drawRoundRect(31,93,80,18,15);
				endFill();
			}
			
		}
		
		private function initTextBg2():void
		{
			with (textBg.graphics)
			{
				clear();
				lineStyle(2,0xFFFFFF,.8);
				beginFill(0,.8)
				drawRoundRect(31,74,80,16,15);
				endFill();
			}
			
		}
		
		private function initTitle():void
		{
			title = TextTool.getTextField(80,20,12,0xFFFFCD,false,true,TextFormatAlign.CENTER);
			
			title.x = 30;
			title.y = - 5;
			addChild(title);
		}
		
		
		
		public function showCountTimer(isShow:Boolean):void
		{
			if (isShow)
			{
				addChild(countTimer);
				countTimer.update(circleTimer.leftTime.toString());
			}
			else
			{
				DisplayTool.removeChild(this,countTimer);
			}
		}
		
		public function updateHeadImg(imgUrl:String):void
		{
			
			headImg.loadImg(imgUrl);
		}
		
		public function updateMoney(v:String):void
		{
			money.text = "$" + v;
		}
		
		public function updateAction(action:String):void
		{
			state.text = action;
			if (action == "")
			{
				initTextBg2();	
			}
			else
			{
				initTextBg();
			}
		}
		
		public function updateLeftSecond(v:int):void
		{
			if (v > 0)
			{
				circleTimer.leftTime = v;
				circleTimer.stop();
				circleTimer.start();
				circleTimer.visible = true;
			}
			else
			{
				circleTimer.stop();
				circleTimer.visible = false;
			}
		}
		
		public function updateTotalTime(v:int):void
		{
			circleTimer.totalTime = v;
		}
		
		public function updateTalk(v:String):void
		{
			sitPrompt.updateContent(v,this);
			_view.tableLayer.addTalk(sitPrompt);
			
		}
		
		public function updateItemAnimate(v:DisplayObject):void
		{
			
			clearItemAnimate();
			currentItemAnimate = v;
			
			v.x = this.width - 30;
			v.y = 20;
			addChild(v);
			
			v.addEventListener(Event.COMPLETE,onAnimateComplete);
		}
		
		private function clearItemAnimate():void
		{
			if (currentItemAnimate)
			{
				(currentItemAnimate as MovieClip).stop();
				DisplayTool.removeChild(currentItemAnimate.parent,currentItemAnimate);
				currentItemAnimate.removeEventListener(Event.COMPLETE,onAnimateComplete);
				currentItemAnimate = null;
			}
		}
		
		private function onAnimateComplete(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE,onAnimateComplete);
			DisplayTool.removeChild(this,event.target as DisplayObject);
		}
		
		public function updatePlayState(state:int):void
		{
			
		}
		
		public function updateVip(v:int):void
		{
			DisplayTool.removeChild(this,vipImg);
			
			switch(v)
			{
				case 1:
				case 2:
				case 3:
				case 4:
					vipImg = Resource.getImg("face_vip" + v);
					vipImg.x = bg.x + bg.width + 1;
					vipImg.y = bg.y + 5;
					addChild(vipImg);
					break;
					
			}
			
		}
		
		public function updateMoneyChangeAnimate(v:int):void
		{
			var count:CountNumber;
			if (v<0)
			{
				count = new CountNumber(CountNumber.GREEN);
			}
			else
			{
				count = new CountNumber(CountNumber.GOLD);
			}
			count.addEventListener(Event.COMPLETE,onMoneyChangeComplete);
			count.x = 50;
			
			if (v<0)
			{
				count.show("-$" + Math.abs(v),60,40);
			}
			else
			{
				count.show("+$" + Math.abs(v),60,40);
			}
			addChild(count);
		}
		
		private function onMoneyChangeComplete(event:Event):void
		{
			event.target.destroy();
			DisplayTool.removeChild(this,event.target as DisplayObject);
		}
		
		public function startPlay():void
		{
			circleTimer.start();
		}
		
		public function stopPlay():void
		{
			circleTimer.stop();
		}
		
		public function updateTitle(v:String):void
		{
			title.text = v;
		}
		
		public function updateAutoState(state:int):void
		{
			autoImg.visible = Boolean(state);
		}
		
		public function updateHoldCards(v:int):void
		{
			if (v == 0)
			{
				leftCardBg.visible = false;
				left.visible = false;
			}
			else
			{
				leftCardBg.visible = true;
				left.visible = true;
			}
			
			
			if (v - lastHoldCards == 1)
			{
				sitControl.showGiveCardAnimate(sitId);
			}
			
			lastHoldCards = v;
			left.text = v + "张";
			if (v == 1)
			{
				if (dan.parent == null)
				{
					dan.gotoAndPlay(1);
				}
				addChild(dan);
			}
			else
			{
				DisplayTool.removeChild(this,dan as DisplayObject);
				dan.stop();
			}
		}
		
		public function get self():DisplayObjectContainer
		{
			
			return this;
		}
		
		public function clear():void
		{
			DisplayTool.removeChild(this,vipImg);
			autoImg.visible = false;
			circleTimer.stop();
			left.text = "";
			money.text = "";
			state.text = "";
			title.text = "";
			headImg.clear();
		}
	}
}